package com.zkc.blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zkc.blog.mapper.UserMapper;
import com.zkc.blog.pojo.User;
import com.zkc.blog.pojo.UserExample;
import com.zkc.blog.service.UserService;
import com.zkc.blog.uilt.MyUilt;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	UserMapper userMapper;
	
	@Override
	public User checkUser(String name, String pwd) {
		// TODO Auto-generated method stub
		//转换pwd为MD5在判断
		pwd=MyUilt.md5(pwd);
		
		UserExample userEx=new UserExample();
		userEx.createCriteria().andUsernameEqualTo(name).andPasswordEqualTo(pwd);
		List<User> users=userMapper.selectByExample(userEx);
		if(users!=null&&users.size()>0){
			return users.get(0);
		}
		return null;
	}

	@Override
	public List<User> getUserList(String namekey) {
		UserExample ex=new UserExample();
		namekey="%"+namekey+"%";
		ex.createCriteria().andUsernameLike(namekey);
		List<User> users=userMapper.selectByExample(ex);
		return users;
	}

	@Override
	public User getUserById(Integer id) {
		User user=userMapper.selectByPrimaryKey(id);
		return user;
	}

	@Override
	public void insertUser(User u) {
		// TODO Auto-generated method stub
		u.setPassword(MyUilt.md5(u.getPassword()));
		userMapper.insert(u);
	}

	@Override
	public void deleteUserByIds(List<Integer> ids) {
		UserExample ex=new UserExample();
		ex.createCriteria().andUseridIn(ids);
		userMapper.deleteByExample(ex);
	}

	@Override
	public void updateUser(User u) {
		// TODO Auto-generated method stub
		u.setPassword(MyUilt.md5(u.getPassword()));
		userMapper.updateByPrimaryKey(u);
	}

}
